﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Service;

namespace Cashier.goods
{
    public partial class WarehouseListControl : UserControl, IDataGridViewData, IReloadData, IPrinter
    {
        private MainForm mainform;
        private static WarehouseListControl warehouseListControl;
        private WarehouseService warehouseService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        private WarehouseListControl(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.dataGridView1.ImplementsInstance = this;
            this.warehouseService = new WarehouseService();
            
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 返回控件实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static WarehouseListControl GetWarehouseListControl(MainForm mainform)
        {
            if (warehouseListControl == null)
                warehouseListControl = new WarehouseListControl(mainform);
            warehouseListControl.RefreshDataGridViewData();
            return warehouseListControl;
        }

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            try
            {
                if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                {
                    long supplierid = (long)dataGridView1.SelectedRows[0].Cells[0].Value;
                    WarehouseAddForm warehouseAddForm = new WarehouseAddForm(this, null, supplierid);
                    warehouseAddForm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("warehouse_no", "编号", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("name", "名称", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("province", "所在省", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("city", "所在市", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("street", "详细地址", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("person", "负责人", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("mobile", "手机", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("createtime", "创建日期", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String name = textBox_name.Text.Trim();                
                DataTable dt = warehouseService.GetWarehouseTable(pageindex, pagesize, name, null, null);
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count>0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String name = textBox_name.Text.Trim();
                record = warehouseService.GetWarehouseRecord(name, null, null);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 重载事件

        /// <summary>
        /// 重载键盘事件
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool b = base.ProcessDialogKey(keyData);
            if (keyData == Keys.Insert)
            {
                WarehouseAddForm warehouseAddForm = new WarehouseAddForm(this, null, 0);
                warehouseAddForm.ShowDialog();
            }
            return b;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 双击单元格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                long warehouseid = (long)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                WarehouseAddForm warehouseAddForm = new WarehouseAddForm(this, null, warehouseid);
                warehouseAddForm.ShowDialog();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_add_Click(object sender, EventArgs e)
        {
            WarehouseAddForm warehouseAddForm = new WarehouseAddForm(this, null, 0);
            warehouseAddForm.ShowDialog();
        }

        /// <summary>
        /// 文本框键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;
            if (keyValue == 13)
            {
                this.RefreshDataGridViewData();
            }
        }

        #endregion

        #region IReloadData 成员

        void IReloadData.ReloadDataGridViewData(object param)
        {
            this.RefreshDataGridViewData();
        }

        #endregion

        #region IPrinter 成员

        DataGridView IPrinter.GetAllRows()
        {
            try
            {
                ((IDataGridViewData)this).GetBindingData(0, 0, 0);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return this.dataGridView1;
        }

        DataGridView IPrinter.GetSelectedRows()
        {
            return this.dataGridView1;
        }

        void IPrinter.VisibleColumn(string columnname)
        {
            try
            {
                if (this.dataGridView1.Columns[columnname].Visible)
                    this.dataGridView1.Columns[columnname].Visible = false;
                else
                    this.dataGridView1.Columns[columnname].Visible = true;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion
    }
}
